package jers.util.pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import jers.JERS;
import jers.util.ThreadPool;

/**
 * Classe responsavel por implementar um pool de threads para o sistema.
 * Esta utiliza os recursos de pool das implementacoes de 
 * {@link ExecutorService} padroes do Java. 
 * 
 * @see {@link ThreadPool}
 * 
 * @author 
 * 		<a href="mailto:felipe.zappala@gmail.com">
 * 			Luis Felipe Zappala
 * 		</a> [ <a href="http://lncc.br/ist">ISTCC-P</a> / 2009 ]
 */
public class SimpleThreadPool implements ThreadPool {
	
	/** Pool real de threads*/
	private ExecutorService executor;

	/**
	 * Construtor padrao
	 */
	public SimpleThreadPool() {
		start();
	}
	
	/**
	 * Executa um {@link Runnable} no pool.
	 */
	public void execute(Runnable runnable) {
		this.executor.execute(runnable);
	}

	public void start() {
		int defaultSize = 10;
		try {
			defaultSize = Integer.parseInt(JERS.config
				.getProperty("jers.service.threadpool.size"));
				
		} catch (Exception e) {
			// Nada fazer
		}
		this.executor = Executors.newFixedThreadPool(defaultSize);
	}

	public void stop() {
		this.executor.shutdown();
	}

	public boolean isStarted() {
		return !this.executor.isShutdown();
	}
	
	public boolean isStopped() {
		return this.executor.isShutdown();
	}

}
